package com.chuanjiabao.wisdom.util;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.ui.view.RoundedCornersTransform;

import java.math.BigDecimal;

public class GlideUtil {

    //设置有弧度的网络图片
    public static void setRoundUriMethod(Context context, String url, ImageView image, int radio) {
        RoundedCornersTransform transform = new RoundedCornersTransform(context, radio);
        transform.setNeedCorner(true, true, true, true);
        RequestOptions options = new RequestOptions().transform(transform);
        Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(options)
                .thumbnail(0.1f)
                .error(R.mipmap.icon_noimge)
                .into(image);
    }

    //设置有弧度的网络图片
    public static void setRoundUriMethod(
            Context context,
            String url,
            ImageView image,
            @DrawableRes int error,
            int radio
    ) {
        RoundedCornersTransform transform = new RoundedCornersTransform(context, radio);
        transform.setNeedCorner(true, true, true, true);
        RequestOptions options = new RequestOptions().transform(transform);
        Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(options)
                .thumbnail(0.1f)
                .error(error)
                .into(image);
    }

    //设置有弧度的本地图片
    public static void setRoundIDMethod(Context context, int id, ImageView image, int radio) {
        Glide.with(context)
                .asBitmap()
                .load(id)
                .thumbnail(0.1f)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(radio)))
                .into(image);
    }

    //设置圆角网络图片
    public static void setCircleMethod(Context context, String url, ImageView image) {
        RequestOptions requestOptions = RequestOptions.circleCropTransform();
        Glide.with(context)
                .asBitmap() // some .jpeg files are actually gif
                .load(url)
                .thumbnail(0.1f)
                .placeholder(R.mipmap.icon_default_user)
                .error(R.mipmap.icon_default_user)
                .apply(requestOptions)
                .into(image);
    }

    //单纯的设置图片
    public static void setImgMethod(int id, ImageView image) {
        image.setImageResource(id);
    }

    //设置本地图片
    public static void setLocalMethod(Context context, String url, ImageView image) {
        Glide.with(context)
                .load(url)
                .placeholder(R.mipmap.icon_noimge)
                .error(R.mipmap.icon_noimge)
                .into(image);
    }


    //单纯的加载网络图片
    public static void setUrlImg(
            Context context,
            String url,
            String name,
            final ImageView image
    ) {
        Glide.with(context)
                .asBitmap()
                .load(new StableGlideUrl(url, name))
                .thumbnail(0.1f)
                .placeholder(R.mipmap.icon_noimge)
                .error(R.mipmap.icon_noimge)
                .into(image);
    }

    /**
     * 格式化单位
     *
     * @param size size
     * @return size
     */
    public static String getFormatSize(long size) {

        double kiloByte = size * 1.0 / 1024;
        if (kiloByte < 1) {
            return size + "B";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);

        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }

}
